package com.appspot.diasporajava.manager;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.appspot.diasporajava.dao.PodDao;
import com.appspot.diasporajava.entities.Pod;
import com.appspot.diasporajava.entities.User;
import com.appspot.diasporajava.federation.WebFinger;
import com.appspot.diasporajava.federation.WebFingerVO;

@Component
public class FederationManager {
	
	@Autowired
	WebFinger webFinger;
	
	@Autowired
	PodDao podDao;
	
	public User findUser(String userEmail) throws IOException {
		String template = null;
		userEmail = userEmail.replaceAll("-", ".");
		String[] parts = userEmail.split("@");
		
		List<Pod> pods = podDao.findByUrl(parts[1]);
		if(pods.size()>0){
			template=pods.get(0).getTemplate();
		}else{
			String podUrl = "http://" + parts[1] + "/";
			template = webFinger.getTemplate(podUrl);
		}
		
		
		
		try {
			
			WebFingerVO webFingerVO = webFinger.getUserDescription(parts[0], template);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return null;
	}
}
